<?php
/**
 * Created by PhpStorm.
 * User: 仙女大人
 * Date: 2019/5/23
 * Time: 20:54
 */

namespace app\admin\controller;
use think\Controller;
use think\Session;
use app\admin\model\Img as ImgModel;
use app\admin\model\Nav as NavModel;
use think\Db;

class Nav extends Controller
{
    // 列表页面
    public function index()
    {
        return $this->fetch();
    }

    // 数据请求接口
    public function user()
    {
        $data = Db::name('img')->order('id','desc')->paginate('10');
        $list = $data->toArray();
        return json(["code"=>0,"msg"=>"", "count"=>$list['total'] ,"data"=>$list['data']]);
    }
    //admin/nav/littleUpdate

    // 添加导航
    public function add_nav()
    {
        $data = Db::name('nav')->where('show',0)->order('sort','desc')->select();
        $datav = getTreeOne($data,0);
        $this->assign('data',$datav);
        return view();
    }

    public function addNav()
    {
        $data = input('post.');
        $data['show'] == 'on' ? $data['show'] = 1 : $data['show'] = 0;
        $user = new NavModel();
        if ($user->allowfield(true)->save($data)) {
            return json(['code' => 1, 'data' => $user, 'msg' => 'ok']);
        } else {
            return json(['code' => 2,'msg' => '添加失败']);
        }
    }

    // 即点即改
    public function littleUpdate()
    {
        //dump(input('post.'));
        $id = input('post.id');
        $field = input('post.y');
        $value = input('post.x');
        $res = ImgModel::get($id);
        $res->$field = $value;
        $data = $res->save();
        if(!$data)
        {
            return json(['code' => 2,'msg' => '修改失败']);
        }else
        {
            return json(['code' => 1, 'data' => $data, 'msg' => 'ok']);
        }
    }

    // 监听状态修改
    public function fieldUpdate()
    {
        $id = input('post.id');
        $is_visible = input('post.is_visible');
        $is_visible == 'true' ? $is_visible = 1 : $is_visible = 0;
        $res = ImgModel::get($id);
        $visible = $res->allowField(true)->save(['is_visible' => $is_visible]);
        if(!$visible)
        {
            return json(['code' => 2,'msg' => '修改失败']);
        }else
        {
            return json(['code' => 1, 'data' => $visible, 'msg' => 'ok']);
        }
    }

    public function add()
    {
        return view();
    }

    // 处理添加
    public function add_do()
    {
        //入库之前进行数据验证
        $data = input('post.');
        $data['is_visible'] = input('post.is_visible');
        $data['is_visible'] == 'on' ? $data['is_visible'] = 1 : $data['is_visible'] = 0;
        $user = new ImgModel();
        //过滤非数据表字段的数据allowField()方法
        if ($user->allowfield(true)->save($data)) {
            return json(['code' => 1, 'data' => $user, 'msg' => 'ok']);
        } else {
            return json(['code' => 2,'msg' => '添加失败']);
        }
    }

    //修改页面
    public function update(){
        $id = input('get.');
        $data = ImgModel::find($id);
        $arr = $data->toArray();
        $this->assign('data',$arr);
        return view();
    }

    // 修改处理
    public function doUpdate()
    {
        $data = input();
        $id = $data['id'];
        $blog = new ImgModel();
        if ($blog->update(['id'=>$id])->allowfield(true)->save($data)) {
            return json(['code' => 1, 'data' => $blog, 'msg' => 'ok']);
        } else {
            return json(['code' => 2,'msg' => '修改失败']);
        }
    }

    // 删除操作
    public function del()
    {
        $data = input('post.');
        $id = $data['id'];
        $res = ImgModel::destroy($id);
        if($res)
        {
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else
        {
            return json(['code' => 2,'msg' => '删除失败']);
        }
    }

    public function delAll()
    {
        $id = rtrim(input('post.id'),',');
        // rtrim — 删除字符串末端的空白字符（或者其他字符）
        $result = Db::execute("delete from think_img where id in ($id) ");
        if ($result) {
            return json(['code' => 1, 'data' => $result, 'msg' => 'ok']);
        } else {
            return json(['code' => 2,'msg' => '批删失败']);
        }
    }

    // 处理文件上传
    public function uploadLayui()
    {
        $fileObj = request()->file('file');//获取文件信息 （对象）
        $path =  'static/uploads/slide_upload';//指定文件目录
        //不存在就自行创建文件夹
        if(!is_dir($path)){
            mkdir($path, 0755, true);
        }
        $info = $fileObj->move($path);//保存在目录文件下
        if ($info && $info->getPathname())
        {
            return json(['code' => 1, 'msg'=>'上传成功', 'data' =>['src'=>'/'.$info->getPathname()]]);
        } else {
            return json(['code' => 2,'msg' => '上传失败']);//获取错误信息
        }
    }

}